import { BonusRules } from './bonusRule';
let _BonusRules = BonusRules;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import BonusRuleMock from './mk/mng/card/bonusRuleMock';
 * BonusRuleMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mk/mng/card/bonusRule/listKey').reply(config => {
      let params = config.params;
      let mockBonusRules = _BonusRules.filter(bonusRule => {
      	if(params.costMoneyUnit||params.costMoneyUnit==''){ if( bonusRule.costMoneyUnit.indexOf(params.costMoneyUnit)>=0) return true;}
      	if(params.increaseBonus||params.increaseBonus==''){ if( bonusRule.increaseBonus.indexOf(params.increaseBonus)>=0) return true;}
      	if(params.maxIncreaseBonus||params.maxIncreaseBonus==''){ if( bonusRule.maxIncreaseBonus.indexOf(params.maxIncreaseBonus)>=0) return true;}
      	if(params.initIncreaseBonus||params.initIncreaseBonus==''){ if( bonusRule.initIncreaseBonus.indexOf(params.initIncreaseBonus)>=0) return true;}
      	if(params.costBonusUnit||params.costBonusUnit==''){ if( bonusRule.costBonusUnit.indexOf(params.costBonusUnit)>=0) return true;}
      	if(params.reduceMoney||params.reduceMoney==''){ if( bonusRule.reduceMoney.indexOf(params.reduceMoney)>=0) return true;}
      	if(params.leastMoneyToUseBonus||params.leastMoneyToUseBonus==''){ if( bonusRule.leastMoneyToUseBonus.indexOf(params.leastMoneyToUseBonus)>=0) return true;}
      	if(params.maxReduceBonus||params.maxReduceBonus==''){ if( bonusRule.maxReduceBonus.indexOf(params.maxReduceBonus)>=0) return true;}
      	if(params.cardPkId||params.cardPkId==''){ if( bonusRule.cardPkId.indexOf(params.cardPkId)>=0) return true;}
      	if(params.id||params.id==''){ if( bonusRule.id.indexOf(params.id)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockBonusRules
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mk/mng/card/bonusRule/list').reply(config => {
      let params = config.params;
      let mockBonusRules = _BonusRules.filter(bonusRule => {
      	var isTrue=false;
      	if(params.costMoneyUnit||params.costMoneyUnit==''){if(bonusRule.costMoneyUnit.indexOf(params.costMoneyUnit)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.increaseBonus||params.increaseBonus==''){if(bonusRule.increaseBonus.indexOf(params.increaseBonus)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.maxIncreaseBonus||params.maxIncreaseBonus==''){if(bonusRule.maxIncreaseBonus.indexOf(params.maxIncreaseBonus)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.initIncreaseBonus||params.initIncreaseBonus==''){if(bonusRule.initIncreaseBonus.indexOf(params.initIncreaseBonus)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.costBonusUnit||params.costBonusUnit==''){if(bonusRule.costBonusUnit.indexOf(params.costBonusUnit)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.reduceMoney||params.reduceMoney==''){if(bonusRule.reduceMoney.indexOf(params.reduceMoney)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.leastMoneyToUseBonus||params.leastMoneyToUseBonus==''){if(bonusRule.leastMoneyToUseBonus.indexOf(params.leastMoneyToUseBonus)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.maxReduceBonus||params.maxReduceBonus==''){if(bonusRule.maxReduceBonus.indexOf(params.maxReduceBonus)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.cardPkId||params.cardPkId==''){if(bonusRule.cardPkId.indexOf(params.cardPkId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.id||params.id==''){if(bonusRule.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockBonusRules
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockBonusRules.length;
      mockBonusRules = mockBonusRules.filter((bonusRule, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockBonusRules,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mk/mng/card/bonusRule/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _BonusRules = _BonusRules.filter(bonusRule => bonusRule.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mk/mng/card/bonusRule/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _BonusRules = _BonusRules.filter(bonusRule => !params.includes(bonusRule.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mk/mng/card/bonusRule/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { costMoneyUnit,increaseBonus,maxIncreaseBonus,initIncreaseBonus,costBonusUnit,reduceMoney,leastMoneyToUseBonus,maxReduceBonus,cardPkId,id } = params;
      _BonusRules.some(bonusRule => {
        if (bonusRule.id === id) {
			bonusRule.costMoneyUnit = costMoneyUnit;
			bonusRule.increaseBonus = increaseBonus;
			bonusRule.maxIncreaseBonus = maxIncreaseBonus;
			bonusRule.initIncreaseBonus = initIncreaseBonus;
			bonusRule.costBonusUnit = costBonusUnit;
			bonusRule.reduceMoney = reduceMoney;
			bonusRule.leastMoneyToUseBonus = leastMoneyToUseBonus;
			bonusRule.maxReduceBonus = maxReduceBonus;
			bonusRule.cardPkId = cardPkId;
			bonusRule.id = id
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mk/mng/card/bonusRule/add').reply(config => {
      var params = JSON.parse( config.data );
      let { costMoneyUnit,increaseBonus,maxIncreaseBonus,initIncreaseBonus,costBonusUnit,reduceMoney,leastMoneyToUseBonus,maxReduceBonus,cardPkId,id } = params;
      _BonusRules.push({
		costMoneyUnit:costMoneyUnit,
		increaseBonus:increaseBonus,
		maxIncreaseBonus:maxIncreaseBonus,
		initIncreaseBonus:initIncreaseBonus,
		costBonusUnit:costBonusUnit,
		reduceMoney:reduceMoney,
		leastMoneyToUseBonus:leastMoneyToUseBonus,
		maxReduceBonus:maxReduceBonus,
		cardPkId:cardPkId,
		id:id
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};